package com.uaroadshow.entity.trade;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import com.demo2do.core.utils.DateFormatUtils;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

/**
 * 
 * @author mohanlan
 *
 */
@Entity
@Table(name = "product")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@JsonIgnoreProperties(value = { "hibernate_lazy_initializer", "handler", "fieldHandler" })  
public class Product {
    
    @Id
    @GeneratedValue
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
    @JsonIdentityReference(alwaysAsId = true)
    private Product parent;
    
    /** 课程代码 */
    private String code;
    
    /** 名称 */
    private String name;
    
    /** 介绍 */
    private String description;
    
    /** 价格 */
    private int price;
    
    /** 课程开始时间 */
    private Date startTime;
    
    /** 课程结束时间 */
    private Date endTime;
    
    /** 导师头像 */
    private String coachAvatar;
    
    /** 导师姓名 */
    private String coachName;
    
    /** 导师介绍 */
    private String coachDescription;
    
    /** 装备包 */
    private String equipmentPackage;
    
    /** 装备包价值 */
    private String equipmentPackagePrice;
    
    @Column(name = "is_disabled")
    private boolean disabled;
    
    /**
     * The default constructor
     */
    public Product() {
    }

    /**
     * @return
     */
    @Transient
    @JsonIgnore
    public String getIdString() {
        return String.valueOf(id);
    }
    
    /**
     * @return
     */
    @Transient
    @JsonIgnore
    public boolean isParent() {
        return parent == null;
    }
    
    /**
     * @return
     */
    @Transient
    @JsonIgnore
    public List<String> getChildCodes() {
        return Arrays.asList(code.split("|"));
    }
    
    /**
     * @return
     */
    @Transient
    public boolean isSingleAvatar() {
        return StringUtils.contains(coachAvatar, "|");
    }
    
    /**
     * @return
     */
    @Transient
    public String[] getCoachAvatarList() {
        return StringUtils.split(coachAvatar, "|");
    }
    
    /**
     * 
     * @return
     */
    @Transient
    public String[] getCoachNameList() {
        return StringUtils.split(coachName, "|");
    }
    
    /**
     * @return
     */
    @Transient
    public String[] getCoachDescriptionList() {
        return StringUtils.split(coachDescription, "|");
    }
    
    /**
     * 课程开始时间
     * 
     * @return
     */
    @Transient
    public String getOccurTime() {
        return DateFormatUtils.format(this.startTime, "yyyy年MM月dd日") + " " + DateFormatUtils.format(this.startTime, "HH:mm");
    }
    
    /**
     * 上课区间
     * 
     * @return
     */
    @Transient
    public String getIntervalTime() {
        return DateFormatUtils.format(this.startTime, "HH:mm") + "-" + DateFormatUtils.format(this.endTime, "HH:mm");
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }
    
    /**
     * @return
     */
    public Product getParent() {
        return parent;
    }
    
    /**
     * @return
     */
    public String getCode() {
        return code;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @return the description
     */
    public String getDescription() {
        return description;
    }

    /**
     * @return the price
     */
    public int getPrice() {
        return price;
    }

    /**
     * @return the startTime
     */
    public Date getStartTime() {
        return startTime;
    }

    /**
     * @return the endTime
     */
    public Date getEndTime() {
        return endTime;
    }

    /**
     * @return the coachAvatar
     */
    public String getCoachAvatar() {
        return coachAvatar;
    }

    /**
     * @return the coachName
     */
    public String getCoachName() {
        return coachName;
    }

    /**
     * @return the coachDescription
     */
    public String getCoachDescription() {
        return coachDescription;
    }

    /**
     * @return the equipmentPackage
     */
    public String getEquipmentPackage() {
        return equipmentPackage;
    }
    
    /**
     * @return
     */
    public String getEquipmentPackagePrice() {
        return equipmentPackagePrice;
    }

    /**
     * @return the disabled
     */
    public boolean isDisabled() {
        return disabled;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }
    
    /**
     * @param parent
     */
    public void setParent(Product parent) {
        this.parent = parent;
    }

    /**
     * @param code
     */
    public void setCode(String code) {
        this.code = code;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @param description the description to set
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * @param price the price to set
     */
    public void setPrice(int price) {
        this.price = price;
    }

    /**
     * @param startTime the startTime to set
     */
    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    /**
     * @param endTime the endTime to set
     */
    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    /**
     * @param coachAvatar the coachAvatar to set
     */
    public void setCoachAvatar(String coachAvatar) {
        this.coachAvatar = coachAvatar;
    }

    /**
     * @param coachName the coachName to set
     */
    public void setCoachName(String coachName) {
        this.coachName = coachName;
    }

    /**
     * @param coachDescription the coachDescription to set
     */
    public void setCoachDescription(String coachDescription) {
        this.coachDescription = coachDescription;
    }

    /**
     * @param equipmentPackage the equipmentPackage to set
     */
    public void setEquipmentPackage(String equipmentPackage) {
        this.equipmentPackage = equipmentPackage;
    }
    
    /**
     * @param equipmentPackagePrice
     */
    public void setEquipmentPackagePrice(String equipmentPackagePrice) {
        this.equipmentPackagePrice = equipmentPackagePrice;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }
    
}